package com.example.myprojectoffeixue;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class AddCJshebeiActivity extends Activity {
    //添加和场景绑定的设备的界面，包括选中设备和指定选中设备的状态，
    // 主要注意的是设备对象不能直接用从服务器获取的对象，
    // 不然会在操作场景设备的状态时与原对象混乱
    private ArrayList<SheBeiClass> sb;
    private MyApplication m;
    private Map<Integer, Integer> map1 = new HashMap<>();
    private Map<Integer, Integer> map2 = new HashMap<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_cjshebei);
        info();
    }

    private void info() {
        Intent it = getIntent();
        LinearLayout iv = (LinearLayout) findViewById(R.id.fanhui_addcjshebei);
        iv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView queding = (TextView) findViewById(R.id.bt_queding);
        m = (MyApplication) getApplication();
        sb = new ArrayList<SheBeiClass>();
        sb.addAll(m.lsb);
       // sb.addAll(m.lsb1);
        sb.addAll(m.lsb3);
        sb.addAll(m.lsb5);
        sb.addAll(m.lsb8);
        queding.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                m.map.clear();
                int x=0;
                for (int i = 0; i < sb.size(); i++) {
                    if (map2.get(i) == 1) {
                        x=1;
                        m.map.put(i, map1.get(i));
                        finish();
                    }
                }
                if(x==0){
                    ToastUtils.showToast(AddCJshebeiActivity.this,"请勾选要添加的设备");
                }
            }
        });

        for (int i = 0; i < sb.size(); i++) {
            if (m.map.containsKey(i)) {
                map1.put(i, m.map.get(i));
                map2.put(i, 1);
            } else {
                map1.put(i, 0);
                map2.put(i, 0);
            }
        }
        ListView gd = (ListView) findViewById(R.id.lv_addchangjingshebei);
        gd.setAdapter(new TianjiaCJsbadapter(this, sb, map1, map2));
    }
}